package cn.edu.sjtu.se.dslab.controller;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import cn.edu.sjtu.se.dslab.component.data.DataType;
import cn.edu.sjtu.se.dslab.component.data.DomainEntity;
import cn.edu.sjtu.se.dslab.component.method.ComponentInterface;
import cn.edu.sjtu.se.dslab.component.method.InterfaceMethod;
import cn.edu.sjtu.se.dslab.component.method.util.InterfaceGenerator;
import cn.edu.sjtu.se.dslab.service.IComponentInterfaceService;
import cn.edu.sjtu.se.dslab.service.IDataTypeService;
import cn.edu.sjtu.se.dslab.service.IDomainEntityService;
import cn.edu.sjtu.se.dslab.service.IInterfaceMethodService;

@Controller
@RequestMapping(value = "/function")
public class InterfaceMethodController {
	
	@Resource(name = "componentInterfaceService")
	IComponentInterfaceService componentInterfaceService;

	@Resource(name = "dataTypeService")
	IDataTypeService dataTypeService;

	@Resource(name = "domainEntityService")
	IDomainEntityService domainEntityService;
	
	@Resource(name = "interfaceMethodService")
	IInterfaceMethodService interfaceMethodService;

	@Resource(name= "interfaceGenerator")
	InterfaceGenerator interfaceGenerator;

	protected Logger logger = LoggerFactory.getLogger(getClass());
	
	@RequestMapping(value = "/{id}/update", method = RequestMethod.GET)
	public ModelAndView updateInterfaceMethodById(
			@PathVariable(value = "id") long id) {
		InterfaceMethod method = interfaceMethodService.getInterfaceMethodById(id);
		List<DataType> _typeList = dataTypeService.getDataTypeList();
		List<DomainEntity> typeList = domainEntityService.getAllDomainEntity();
		typeList.addAll(_typeList);
		Map<Long, String> typeMap = new HashMap<Long, String>();
		for (int i = 0; i < typeList.size(); i++) {
			typeMap.put(typeList.get(i).getId(), typeList.get(i).getName());
		}
		ModelAndView model = new ModelAndView();
		if (method != null) {
			model.addObject("method", method);
			model.addObject("typeList", typeMap);
			model.setViewName("update_function");
		}else {
			model.setViewName("error");
		}
		return model;
	}
	
	/**
	 * 该函数目前只针对于interface对应的function的更新操作
	 * @param id
	 * @param method
	 * @return 对应的interface详细页面
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
	@RequestMapping(value = "/{id}/update", method = RequestMethod.POST)
	public String updateInterfaceMethodById(
			@PathVariable(value = "id") long id,
			InterfaceMethod method) throws FileNotFoundException,
			IOException {
		if (method != null) {
//			long typeId = method.getReturnType().getId();
//			DomainEntity returnType = domainEntityService.getDomainEntityById(typeId);
//			String signature = returnType.getName()+" " +method.getName() +"("+method.getArguments()+");";
//			method.setMethodSigniture(signature);
			interfaceMethodService.updateInterfaceMethod(method);
			//接口对应的函数修改，删除相应的生成文件
			ComponentInterface cInterface = componentInterfaceService.getComponentInterfaceById(method.getcInterface().getId());
			interfaceGenerator.deleteInterfaceFile(cInterface);
			//System.out.println("id:"+method.getId());
			//System.out.println("name:"+method.getName());
			//System.out.println("Arguments:"+method.getArguments());
			//System.out.println("Description:"+method.getDescription());
//			if (method.getReturnType() != null) {
//				System.out.println("return type id:"+method.getReturnType().getId());
//				System.out.println("return type name:"+method.getReturnType().getName());
//			}else {
//				System.out.println("return type is null");
//			}
		}else {
			System.out.println("method is null");
		}
		return "redirect:/"+method.getcInterface().getUri().replace(":", "/");
	}
	
	@RequestMapping(value = "/{id}/delete", method = RequestMethod.GET)
	public String deleteInterfaceMethodById(
			@PathVariable(value = "id") long id)throws FileNotFoundException,
			IOException {
		InterfaceMethod method = interfaceMethodService.getInterfaceMethodById(id);
		String uri  = method.getcInterface().getUri().replace(":", "/");
		long interfaceId = method.getcInterface().getId();
		interfaceMethodService.deleteInterfaceMethodById(id);
		//接口对应的函数修改，删除相应的生成文件
		ComponentInterface cInterface = componentInterfaceService.getComponentInterfaceById(interfaceId);
		interfaceGenerator.deleteInterfaceFile(cInterface);
		return "redirect:/"+uri;
	}
	
}
